package com.atguigu.Flink.datastream.sink;

import com.alibaba.fastjson.JSON;
import com.atguigu.Flink.POJO.Event;
import com.atguigu.Flink.function.ClickSource;
import org.apache.commons.lang3.RandomUtils;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.base.DeliveryGuarantee;
import org.apache.flink.connector.kafka.sink.KafkaRecordSerializationSchema;
import org.apache.flink.connector.kafka.sink.KafkaSink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import javax.annotation.Nullable;
import java.nio.charset.StandardCharsets;

public class Flink03_KafkaSinkWithKey {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<Event> ds = env.addSource(new ClickSource());
        //不带keyxieru
        KafkaSink<Event> kafkaWithKey = KafkaSink.<Event>builder()
                .setBootstrapServers("hadoop102:9092,hadoop103:9092,hadoop104:9092")
                //消息不带key
                .setRecordSerializer(new KafkaRecordSerializationSchema<Event>() {
                    @Nullable
                    @Override
                    public ProducerRecord<byte[], byte[]> serialize(Event event, KafkaSinkContext kafkaSinkContext, Long aLong) {
                        byte[] bytes = event.getUser().getBytes(StandardCharsets.UTF_8);
                        byte[] bytes1 = JSON.toJSONString(event).getBytes(StandardCharsets.UTF_8);
                        ProducerRecord<byte[], byte[]> kvProducerRecord = new ProducerRecord<>("test",bytes, bytes1);
                        return kvProducerRecord;
                    }
                })
                //提交保障
                // 至少一次， 消息可能会重复。
                //.setDeliveryGuarantee(DeliveryGuarantee.AT_LEAST_ONCE)
                // 精确一次， 要求开启检查点， 还要设置事务的前缀
                .setDeliveryGuarantee(DeliveryGuarantee.EXACTLY_ONCE)
                .setTransactionalIdPrefix("flink" + RandomUtils.nextInt(1, 100))
                //如果还有别的配置需要设置，使用如下的方法
                //.setProperty(ProducerConfig.ACKS_CONFIG, "-1")
                .setProperty(ProducerConfig.TRANSACTION_TIMEOUT_CONFIG, "300000")  //5分钟
                .build();
        ds.sinkTo(kafkaWithKey);


        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
